package com.cmcc.groupcontacts.firewall.business;

import java.util.ArrayList;

import com.cmcc.groupcontacts.firewall.dao.KeywordsDAO;
import com.cmcc.groupcontacts.firewall.dao.model.KeywordsVO;
import com.cmcc.groupcontacts.util.ExceptionHandler;
import com.cmcc.groupcontacts.util.StringUtil;

/**
 * 拦截设置业务处理类
 * 
 * @author 
 * 
 */
public class FirewallConfigBusiness extends BaseBusiness {
	private KeywordsDAO keywordsDAO;

	public FirewallConfigBusiness() {
		super();

		keywordsDAO = new KeywordsDAO();
	}

	/**
	 * 获取所有信息拦截关键字
	 * 
	 * @return
	 */
	public ArrayList<KeywordsVO> getKeywordsList() {
		return keywordsDAO.query(false, KeywordsVO.MetaData.ALL_COLS, null, null, null, null, null, null);
	}

	/**
	 * 获取信息拦截关键字
	 * 
	 * @param keywords
	 * @return
	 */
	public KeywordsVO get(String keywords) {
		String where = StringUtil.format("{0}='{1}'", KeywordsVO.MetaData.KEYWORDS, keywords);
		ArrayList<KeywordsVO> keywordsList = keywordsDAO.query(false, KeywordsVO.MetaData.ALL_COLS, where, null, null, null, null, null);
		return (keywordsList == null || keywordsList.size() == 0) ? null : keywordsList.get(0);
	}

	/**
	 * 插入信息拦截关键字
	 * 
	 * @param keywords
	 * @return
	 */
	public long insert(KeywordsVO keywords) {
		try {
			return keywordsDAO.add(keywords);
		} catch (Exception e) {
			ExceptionHandler.handleAcceptable(e, "插入信息拦截关键字时出错");
			return -1;
		}
	}

	/**
	 * 更新信息拦截关键字
	 * 
	 * @param keywords
	 * @return
	 */
	public long update(KeywordsVO keywords) {
		try {
			return keywordsDAO.update(keywords, KeywordsVO.MetaData.ID + "=" + keywords.getId(), null);
		} catch (Exception e) {
			ExceptionHandler.handleAcceptable(e, "更新信息拦截关键字时出错");
			return -1;
		}
	}

	/**
	 * 删除指定的信息拦截关键字
	 * 
	 * @param id
	 * @return
	 */
	public long delete(Integer id) {
		return keywordsDAO.del(KeywordsVO.MetaData.ID + "=" + id, null);
	}
}
